/*
 * @(#)StringUtil.java
 *
 * @Company <Jinher Technology Development Company LTD.>
 */
package com.cn.system.common.util;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.commons.lang.StringUtils;

// String工具类

public class StringUtil {
	
	public static String changeList2Str(List<String> list){
		StringBuffer strBuffer = null;
		if(list!=null && list.size() > 0){
			strBuffer = new StringBuffer();
			for (String str : list) {
				strBuffer.append(","+str);
			}
		}
		return  strBuffer!=null?strBuffer.substring(1).toString():"";
	}
	
	public static void changeList2StrBuffer(List<String> list,StringBuffer strBuffer){
		if(list!=null && list.size() > 0){
			for (String str : list) {
				strBuffer.append(","+str);
			}
		}
	}

	public static boolean isEmpty(String str) {
		return StringUtils.isEmpty(str);
	}

	public static boolean isNotEmpty(String str) {
		return StringUtils.isNotEmpty(str);
	}

	public static boolean isBlank(String str) {
		return StringUtils.isBlank(str);
	}

	public static boolean isNotBlank(String str) {
		return StringUtils.isNotBlank(str);
	}

	public static String trim(String str) {
		return StringUtils.trim(str);
	}

	public static String trimToNull(String str) {
		return StringUtils.trimToNull(str);
	}

	public static String trimToEmpty(String str) {
		return StringUtils.trimToEmpty(str);
	}

	public static String strip(String str) {
		return StringUtils.strip(str);
	}

	public static String stripToNull(String str) {
		return StringUtils.stripToNull(str);
	}

	public static String stripToEmpty(String str) {
		return StringUtils.stripToEmpty(str);
	}

	public static String strip(String str, String stripChars) {
		return StringUtils.strip(str, stripChars);
	}

	public static String stripStart(String str, String stripChars) {
		return StringUtils.stripStart(str, stripChars);
	}

	public static String stripEnd(String str, String stripChars) {
		return StringUtils.stripEnd(str, stripChars);
	}

	public static String[] stripAll(String[] strs) {
		return StringUtils.stripAll(strs);
	}

	public static String[] stripAll(String[] strs, String stripChars) {
		return StringUtils.stripAll(strs, stripChars);
	}

	public static boolean equals(String str1, String str2) {
		return StringUtils.equals(str1, str2);
	}

	public static boolean equalsIgnoreCase(String str1, String str2) {
		return StringUtils.equalsIgnoreCase(str1, str2);
	}

	public static int indexOf(String str, char searchChar) {
		return StringUtils.indexOf(str, searchChar);
	}

	public static int indexOf(String str, char searchChar, int startPos) {
		return StringUtils.indexOf(str, searchChar, startPos);
	}

	public static int indexOf(String str, String searchStr) {
		return StringUtils.indexOf(str, searchStr);
	}

	public static int ordinalIndexOf(String str, String searchStr, int ordinal) {
		return StringUtils.ordinalIndexOf(str, searchStr, ordinal);
	}

	public static int indexOf(String str, String searchStr, int startPos) {
		return StringUtils.indexOf(str, searchStr, startPos);
	}

	public static int lastIndexOf(String str, char searchChar) {
		return StringUtils.lastIndexOf(str, searchChar);
	}

	public static int lastIndexOf(String str, char searchChar, int startPos) {
		return StringUtils.lastIndexOf(str, searchChar, startPos);
	}

	public static int lastIndexOf(String str, String searchStr) {
		return StringUtils.lastIndexOf(str, searchStr);
	}

	public static int lastIndexOf(String str, String searchStr, int startPos) {
		return StringUtils.lastIndexOf(str, searchStr, startPos);
	}

	public static boolean contains(String str, char searchChar) {
		return StringUtils.contains(str, searchChar);
	}

	public static boolean contains(String str, String searchStr) {
		return StringUtils.contains(str, searchStr);
	}

	public static int indexOfAny(String str, char[] searchChars) {
		return StringUtils.indexOfAny(str, searchChars);
	}

	public static int indexOfAny(String str, String searchChars) {
		return StringUtils.indexOfAny(str, searchChars);
	}

	public static int indexOfAnyBut(String str, char[] searchChars) {
		return StringUtils.indexOfAnyBut(str, searchChars);
	}

	public static int indexOfAnyBut(String str, String searchChars) {
		return StringUtils.indexOfAnyBut(str, searchChars);
	}

	public static boolean containsOnly(String str, char[] valid) {
		return StringUtils.containsOnly(str, valid);
	}

	public static boolean containsOnly(String str, String validChars) {
		return StringUtils.containsOnly(str, validChars);
	}

	public static boolean containsNone(String str, char[] invalidChars) {
		return StringUtils.containsNone(str, invalidChars);
	}

	public static boolean containsNone(String str, String invalidChars) {
		return StringUtils.containsNone(str, invalidChars);
	}

	public static int indexOfAny(String str, String[] searchStrs) {
		return StringUtils.indexOfAny(str, searchStrs);
	}

	public static int lastIndexOfAny(String str, String[] searchStrs) {
		return StringUtils.lastIndexOfAny(str, searchStrs);
	}

	public static String substring(String str, int start) {
		return StringUtils.substring(str, start);
	}

	public static String substring(String str, int start, int end) {
		return StringUtils.substring(str, start, end);
	}

	public static String left(String str, int len) {
		return StringUtils.left(str, len);
	}

	public static String right(String str, int len) {
		return StringUtils.right(str, len);
	}

	public static String mid(String str, int pos, int len) {
		return StringUtils.mid(str, pos, len);
	}

	public static String substringBefore(String str, String separator) {
		return StringUtils.substringBefore(str, separator);
	}

	public static String substringAfter(String str, String separator) {
		return StringUtils.substringAfter(str, separator);
	}

	public static String substringBeforeLast(String str, String separator) {
		return StringUtils.substringBeforeLast(str, separator);
	}

	public static String substringAfterLast(String str, String separator) {
		return StringUtils.substringAfterLast(str, separator);
	}

	public static String substringBetween(String str, String tag) {
		return StringUtils.substringBetween(str, tag);
	}

	public static String substringBetween(String str, String open, String close) {
		return StringUtils.substringBetween(str, open, close);
	}

	public static String[] split(String str) {
		return StringUtils.split(str);
	}

	public static String[] split(String str, char separatorChar) {
		return StringUtils.split(str, separatorChar);
	}

	public static String[] split(String str, String separatorChars) {
		return StringUtils.split(str, separatorChars);
	}

	public static String[] split(String str, String separatorChars, int max) {
		return StringUtils.split(str, separatorChars, max);
	}

	public static String[] splitByWholeSeparator(String str, String separator) {
		return StringUtils.splitByWholeSeparator(str, separator);
	}

	public static String[] splitByWholeSeparator(String str, String separator,
			int max) {
		return StringUtils.splitByWholeSeparator(str, separator, max);
	}

	public static String[] splitPreserveAllTokens(String str) {
		return StringUtils.splitPreserveAllTokens(str);
	}

	public static String[] splitPreserveAllTokens(String str, char separatorChar) {
		return StringUtils.splitPreserveAllTokens(str, separatorChar);
	}

	public static String[] splitPreserveAllTokens(String str,
			String separatorChars) {
		return StringUtils.splitPreserveAllTokens(str, separatorChars);
	}

	public static String[] splitPreserveAllTokens(String str,
			String separatorChars, int max) {
		return StringUtils.splitPreserveAllTokens(str, separatorChars, max);
	}

	public static String join(Object[] array) {
		return StringUtils.join(array);
	}

	public static String join(Object[] array, char separator) {
		return StringUtils.join(array, separator);
	}

	public static String join(Object[] array, String separator) {
		return StringUtils.join(array, separator);
	}

	@SuppressWarnings("unchecked")
	public static String join(Iterator iterator, char separator) {
		return StringUtils.join(iterator, separator);
	}

	@SuppressWarnings("unchecked")
	public static String join(Iterator iterator, String separator) {
		return StringUtils.join(iterator, separator);
	}

	public static String deleteWhitespace(String str) {
		return StringUtils.deleteWhitespace(str);
	}

	public static String removeStart(String str, String remove) {
		return StringUtils.removeStart(str, remove);
	}

	public static String removeEnd(String str, String remove) {
		return StringUtils.removeEnd(str, remove);
	}

	public static String remove(String str, String remove) {
		return StringUtils.remove(str, remove);
	}

	public static String remove(String str, char remove) {
		return StringUtils.remove(str, remove);
	}

	public static String replaceOnce(String text, String repl, String with) {
		return StringUtils.replaceOnce(text, repl, with);
	}

	public static String replace(String text, String repl, String with) {
		return StringUtils.replace(text, repl, with);
	}

	public static String replace(String text, String repl, String with, int max) {
		return StringUtils.replace(text, repl, with, max);
	}

	public static String replaceChars(String str, char searchChar,
			char replaceChar) {
		return StringUtils.replaceChars(str, searchChar, replaceChar);
	}

	public static String replaceChars(String str, String searchChars,
			String replaceChars) {
		return StringUtils.replaceChars(str, searchChars, replaceChars);
	}

	public static String overlay(String str, String overlay, int start, int end) {
		return StringUtils.overlay(str, overlay, start, end);
	}

	public static String chomp(String str) {
		return StringUtils.chomp(str);
	}

	public static String chomp(String str, String separator) {
		return StringUtils.chomp(str, separator);
	}

	public static String chop(String str) {
		return StringUtils.chop(str);
	}

	public static String repeat(String str, int repeat) {
		return StringUtils.repeat(str, repeat);
	}

	public static String rightPad(String str, int size) {
		return StringUtils.rightPad(str, size);
	}

	public static String rightPad(String str, int size, char padChar) {
		return StringUtils.rightPad(str, size, padChar);
	}

	public static String rightPad(String str, int size, String padStr) {
		return StringUtils.rightPad(str, size, padStr);
	}

	public static String leftPad(String str, int size) {
		return StringUtils.leftPad(str, size);
	}

	public static String leftPad(String str, int size, char padChar) {
		return StringUtils.leftPad(str, size, padChar);
	}

	public static String leftPad(String str, int size, String padStr) {
		return StringUtils.leftPad(str, size, padStr);
	}

	public static String center(String str, int size) {
		return StringUtils.center(str, size);
	}

	public static String center(String str, int size, char padChar) {
		return StringUtils.center(str, size, padChar);
	}

	public static String center(String str, int size, String padStr) {
		return StringUtils.center(str, size, padStr);
	}

	public static String upperCase(String str) {
		return StringUtils.upperCase(str);
	}

	public static String lowerCase(String str) {
		return StringUtils.lowerCase(str);
	}

	public static String capitalize(String str) {
		return StringUtils.capitalize(str);
	}

	public static String uncapitalize(String str) {
		return StringUtils.uncapitalize(str);
	}

	public static String swapCase(String str) {
		return StringUtils.swapCase(str);
	}

	public static int countMatches(String str, String sub) {
		return StringUtils.countMatches(str, sub);
	}

	public static boolean isAlpha(String str) {
		return StringUtils.isAlpha(str);
	}

	public static boolean isAlphaSpace(String str) {
		return StringUtils.isAlphaSpace(str);
	}

	public static boolean isAlphanumeric(String str) {
		return StringUtils.isAlphanumeric(str);
	}

	public static boolean isAlphanumericSpace(String str) {
		return StringUtils.isAlphanumericSpace(str);
	}

	public static boolean isAsciiPrintable(String str) {
		return StringUtils.isAsciiPrintable(str);
	}

	public static boolean isNumeric(String str) {
		return StringUtils.isNumeric(str);
	}

	public static boolean isNumericSpace(String str) {
		return StringUtils.isNumericSpace(str);
	}

	public static boolean isWhitespace(String str) {
		return StringUtils.isWhitespace(str);
	}

	public static String defaultString(String str) {
		return StringUtils.defaultString(str);
	}

	public static String defaultString(String str, String defaultStr) {
		return StringUtils.defaultString(str, defaultStr);
	}

	public static String defaultIfEmpty(String str, String defaultStr) {
		return StringUtils.defaultIfEmpty(str, defaultStr);
	}

	public static String reverse(String str) {
		return StringUtils.reverse(str);
	}

	public static String reverseDelimited(String str, char separatorChar) {
		return StringUtils.reverseDelimited(str, separatorChar);
	}

	public static String abbreviate(String str, int maxWidth) {
		return StringUtils.abbreviate(str, maxWidth);
	}

	public static String abbreviate(String str, int offset, int maxWidth) {
		return StringUtils.abbreviate(str, offset, maxWidth);
	}

	public static String difference(String str1, String str2) {
		return StringUtils.difference(str1, str2);
	}

	public static int indexOfDifference(String str1, String str2) {
		return StringUtils.indexOfDifference(str1, str2);
	}

	public static int getLevenshteinDistance(String s, String t) {
		return StringUtils.getLevenshteinDistance(s, t);
	}

	public static String format(String s, String... args) {
		String result = s;
		int index = 0;
		while (result.contains("%s")) {
			if (index == args.length)
				break;

			if (args[index] == null) {
				args[index] = " ";
			}
			result = result.replaceFirst("%s", args[index]);
			index++;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public static String findStr(String str, String standardStr) {
		if (str == null || standardStr == null)
			return "";

		String[] strArray = str.split(",");
		String newStr = "";
		for (int n = 0; n < strArray.length; n++) {
			if (standardStr.indexOf(strArray[n]) == -1)
				newStr += strArray[n] + ",";
		}
		if (!newStr.equals(""))
			newStr = newStr.trim().substring(0, newStr.trim().length() - 1);
		if (newStr.equals(","))
			newStr = "";
		return newStr;
	}

	public static double round(double v, int scale) {

		String temp = "###0.";
		for (int i = 0; i < scale; i++) {
			temp += "0";
		}
		return Double.valueOf(new java.text.DecimalFormat(temp).format(v));
	}

	@SuppressWarnings("unchecked")
	public static Hashtable strTohashtable(String hashtable) {
		// 去左边

		hashtable = hashtable.substring(1, hashtable.length());
		// 去右边

		hashtable = hashtable.substring(0, hashtable.length() - 1);
		// 把字符串按","分割成数组

		String[] table = hashtable.split(",");
		// 创建一新对象

		Hashtable<String, String> ht = new Hashtable<String, String>();
		for (int n = 0; n < table.length; n++) {
			//
			String[] str = table[n].split("=");
			if (str.length >= 2)
				ht.put(str[0].trim(), str[1].trim());
		}
		return ht;
	}

	/**
	 * @param string
	 *            原字符串
	 * @param flag
	 *            往左右加的标志
	 * @param length
	 *            加零后的长度
	 * @return 加零后的字符串
	 * 
	 */
	public static String autoAddZoo(String string, String flag, int length) {
		if (string == null)
			throw new NullPointerException();
		StringBuffer sb = new StringBuffer(length);
		if (string.trim().equals("") || string.trim() == "") {
			for (int n = 0; n < length; n++) {
				sb.append("0");
			}
		} else {
			if (flag.trim().equals("left")) {

				for (int l = 0; l < length - string.length(); l++) {
					sb.append("0");
				}
				sb.append(string);
			}
			if (flag.trim().equals("rigth")) {
				sb.append(string);
				for (int l = 0; l < length - string.length(); l++) {
					sb.append("0");
				}
			}

		}

		return sb.toString();
	}

	/**
	 * @param allvol
	 *            带处理的double型数组 比如double [] allvol ={596.4,552.72,342.72,188.86}
	 * @param moq
	 *            最小包装数
	 * @return int 型数组
	 * 
	 */
	@SuppressWarnings("unchecked")
	public static int[] processByMoq(double[] allvol, int moq) {
		int[] vl = new int[allvol.length]; // 定义初始化返回数组

		if (allvol.length > 0 && moq > 0) {
			boolean isNeedMOQ = false;

			HashMap map = new HashMap();
			HashMap map2 = new HashMap();
			TreeSet ts = new TreeSet(Collections.reverseOrder()); // 实现TreeSet
																	// 反序排列
			double nn = 0.0;
			int t = 1;
			for (int n = 0; n < allvol.length; n++) {
				double a = allvol[n];
				if (a % moq != 0)
					isNeedMOQ = true;
			}
			if (isNeedMOQ) {
				for (int n = 0; n < allvol.length; n++) {
					double a = allvol[n];
					double c = a / moq;

					c = round(c, 5);
					String dnn = String.valueOf(c);
					String[] tt = { "", "" };
					tt[0] = dnn.substring(0, dnn.lastIndexOf("."));
					tt[1] = dnn.substring(dnn.lastIndexOf(".") + 1);
					if (!map.isEmpty()) { // 如果map里不为null 且有了相同key 让key＋t区分
						if (map.containsKey("0." + tt[1])) {
							tt[1] = String.valueOf(Integer.parseInt(tt[1]) + t);
							t += 1;
						}
						if (map2.containsKey(tt[0])) {
							tt[0] = tt[0] + "," + t;
							t += 1;
						}
					}
					map.put("0." + tt[1], tt[0]); // 用小数点后的值做key
					map2.put(tt[0], n); // 用来关联顺序
					nn += Double.parseDouble("0." + tt[1]);
					ts.add("0." + tt[1]); // 存入treestet 来排序

					if (tt[0].indexOf(",") > 0)
						tt[0] = tt[0].substring(0, tt[0].indexOf(","));
					vl[n] = Integer.parseInt(tt[0]) * moq; // 把数据存入按传进来的顺序存入int
															// 数组
															// 这很重要一定的按原来的顺序存入
				}
				nn = round(nn, 0); // 得到多出了几个moq
				Iterator it1 = ts.iterator();
				while (nn > 0 && it1.hasNext()) {
					String tt1 = (String) map.get(it1.next());
					if (tt1.indexOf(",") > 0)
						tt1 = tt1.substring(0, tt1.indexOf(","));
					int d1 = Integer.parseInt(tt1) * moq + moq;
					vl[(Integer) map2.get(tt1)] = d1;
					nn--;
				}
			} else {
				for (int n = 0; n < allvol.length; n++) {
					vl[n] = new Double(allvol[n]).intValue();
				}
			}
		}

		return vl;
	}

	/**
	 * 得到NOKIA当前周数
	 * 
	 * @return
	 * @throws ScriptException
	 */
	public static int getWeek() throws ScriptException {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		Compilable compEngine = (Compilable) engine;
		CompiledScript objectWeekCount = compEngine
				.compile("function getMyWeekCount(){var today = new Date();var beginDay = new Date(today.getFullYear(),0,0);var weekNo = parseInt((today.getTime()-beginDay.getTime())/(7*60*60*24*1000));return eval(weekNo+1);};getMyWeekCount();");
		int wCount = ((Double) objectWeekCount.eval()).intValue();
		return wCount;
	}

	/**
	 * 得到周名称
	 * 
	 * @return
	 * @throws ScriptException
	 */

	public static String getWeekName() throws ScriptException {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		Compilable compEngine = (Compilable) engine;
		CompiledScript objectWeekly = compEngine
				.compile("function getWeek(){today=new Date();function initArray(){this.length=initArray.arguments.length;for(var i=0;i<this.length;i++)this[i+1]=initArray.arguments[i];}var d=new initArray('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');return d[today.getDay()+1];};getWeek();");
		String weekly = objectWeekly.eval().toString();
		return weekly;
	}

	/**
	 * 得到NOKIA年
	 * 
	 * @return
	 * @throws ScriptException
	 */
	public static int getYear() throws ScriptException {
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("javascript");
		Compilable compEngine = (Compilable) engine;
		CompiledScript objectYear = compEngine
				.compile("function getcurrentYear(){var date = new Date();var currentYear = date.getYear();return currentYear};getcurrentYear();");
		return ((Double) objectYear.eval()).intValue() + 1900;
	}

	/**
	 * NOKIA 周变月
	 * 
	 * @param week
	 * @return
	 */
	public static int getMonByWeek(int week) {
		int mon = 0;
		if (week >= 1 && week <= 5)
			mon = 1;
		if (week >= 6 && week <= 9)
			mon = 2;
		if (week >= 10 && week <= 13)
			mon = 3;

		if (week >= 14 && week <= 18)
			mon = 4;
		if (week >= 19 && week <= 22)
			mon = 5;
		if (week >= 23 && week <= 26)
			mon = 6;

		if (week >= 27 && week <= 31)
			mon = 7;
		if (week >= 32 && week <= 35)
			mon = 8;
		if (week >= 36 && week <= 39)
			mon = 9;

		if (week >= 40 && week <= 44)
			mon = 10;
		if (week >= 45 && week <= 48)
			mon = 11;
		if (week >= 49 && week <= 52)
			mon = 12;

		return mon;
	}

	public static int getBeaginWeekByMon(int mon) {
		int week = 1;
		if (mon == 1)
			week = 1;
		if (mon == 2)
			week = 6;
		if (mon == 3)
			week = 10;
		if (mon == 4)
			week = 14;
		if (mon == 5)
			week = 19;
		if (mon == 6)
			week = 23;
		if (mon == 7)
			week = 27;
		if (mon == 8)
			week = 32;
		if (mon == 9)
			week = 36;
		if (mon == 10)
			week = 40;
		if (mon == 11)
			week = 45;
		if (mon == 12)
			week = 49;
		return week;
	}

	public static String getRegionNameToID(String regionName) {
		String id = "";
		if ("EAST".equals(regionName.trim().toUpperCase())) {
			id = "2";
		} else if ("SOUTH".equals(regionName.trim().toUpperCase())) {
			id = "3";
		} else if ("WEST".equals(regionName.trim().toUpperCase())) {
			id = "4";
		} else if ("NORTH".equals(regionName.trim().toUpperCase())) {
			id = "5";
		} else {
			id = "6";
		}
		;
		return id;
	}

	/**
	 * 用地区的编码来换地区的名称
	 * 
	 * @param regionCode
	 *            地区的编码
	 * @return 地区的名称
	 * 
	 */
	public static String getRegincodeToRegion(String regionCode) {

		String region = "";
		if (regionCode != null && !"".equals(regionCode)) {
			if ("002".equals(regionCode)) {
				region = "EAST";
			} else if ("003".equals(regionCode)) {
				region = "SOUTH";
			} else if ("004".equals(regionCode)) {
				region = "WEST";
			} else if ("005".equals(regionCode)) {
				region = "NORTH";
			} else if ("001".equals(regionCode)) {
				region = "region";
			} else
				region = "PRC";
		}
		return region;
	}

	/**
	 * 按部门的编码得到区域的名称
	 * 
	 * @param deptCode
	 * @return
	 */
	public static String getRegionByDeptCode(String deptCode) {
		String region = null;
		if (deptCode != null && !deptCode.trim().equals("")) {
			if ("0034".equals(deptCode)) {
				region = "EAST";
			} else if ("0032".equals(deptCode)) {
				region = "SOUTH";
			} else if ("0031".equals(deptCode)) {
				region = "WEST";
			} else if ("0033".equals(deptCode)) {
				region = "NORTH";
			} else {

			}
		}
		return region;
	}
	
	/**
	 * 组织层级码以及机构码生成方法
	 * //100010021005-->100010021006
	 * @param code
	 * @return
	 */
	public static String makeOrgLevelCode(String maxCode){ 
		String temp = String.valueOf(maxCode);
		switch(temp.length()){
		case 1:
			return "100"+maxCode;
		case 2:
			return "10"+maxCode;
		}
		return null;
	}
    public static String getModuleCode(int code){
		
		String temp = String.valueOf(code);
		switch(temp.length()){
		case 1:
			return "00"+code;
		case 2:
			return "0"+code;
		case 3:
			return String.valueOf(code);
		}
		return null;
	}
    
	/**
	 * 组织层级码生成方法
	 * 输入：将ORGCODE作为参数传入，例如：1003
	 * 输出：在ORGCODE基础上加一即可,例如：1004
	 * 输出SQL:select max(start) from sample where start BETWEEN '10001000' and '10009999'
	 * @param orgCode      该级别的最大层级码信息
	 * @param parOrgCode   父节点的组织层级码数值
	 * @return
	 * 如果在该级别查询到最大的组织层级码，则在该最大值加1，如果没有查询到组织级别码，则认为该组织下没有其他组织
	 * 直接在父级别码后加1000即可
	 * String prefix = "1000";
	 * String suffix = "1999";
	 * author 孙海龙
	 */
	public static String makeOrgLevelCode(String parOrgCode,String maxOrgCode){//10001003
		//最终处理结果
		String LevelCode = "";
		//截取数据信息
		String operateCode = "";
		//上一级组织层级码信息
		String prfixOrgCode = "";
		if(maxOrgCode!=""&&maxOrgCode!=null){
			int length = maxOrgCode.length();
			int prefixLen = length - 4;
			prfixOrgCode = maxOrgCode.substring(0,prefixLen);
			operateCode = maxOrgCode.substring(prefixLen,length);
			long len = new Long(operateCode).intValue()+1;
			String tempCode=String.format("%04d", len);
//			LevelCode = prfixOrgCode + new Long(len).toString();
			LevelCode = prfixOrgCode + tempCode;
		}else{
			LevelCode = parOrgCode +"1000";
		}
		return LevelCode;
	}
	/**
	 * 机构码生成方法
	 * @param parOrgCode  父级别的机构码
	 * @param orgFlag     组织或部门的标识，0为机构，1为部门
	 * @return
	 * 根据父机构的机构码生成新机构的机构码，即：在父机构码的基础上增加“1000”作为新机构的机构码
	 * 只有在创建机构时才能
	 */
	public static String makeOrgCode(String parOrgCode,String orgFlag){
		String orgCode = "";
		if(parOrgCode!=null&&!parOrgCode.equals("")){
			orgCode = parOrgCode + "1000";
		}else{
			orgCode = "1000";
		}
		return orgCode;
	}
	
	/**
	 * 根据组织级别码，机构码生成对应的sql语句
	 * @param token         查询方式，集团内查询或是本机构内查询，如果为0则为集团内查询，如果为1则为机构内查询
	 * @param orgLevelCode  组织级别码
	 * @param orgCode       机构码
	 * @param alias         表的别名,如果存在别名则将别名传入，如果没有别名，则将该变量设置为""即可；
	 * @param switchs       集团化使用开关，0为使用，1为不使用
	 * @return
	 * author 孙海龙
	 * date    2009-10-09
	 * 注释：需要在此sql的前面加上and语句，如果后面还有sql语句，则在后面也需要加上and语句；
	 * 
	 */
	public static String makeOrgCodeSql(String token,String orgLevelCode,String orgCode,String alias,String switchs){
		String orgCodeSql = "";
		if(switchs!=null&&switchs.equals("0")){
			//如果为集团内查询
			if(token.equals("0")){
				if(alias.equals("")||alias==null){
					orgCodeSql = " commOrgLevel like '" + orgLevelCode + "%' AND commOrgIdenty like '" + orgCode + "%' ";
				}else{
					orgCodeSql = " " + alias +".commOrgLevel like '" + orgLevelCode + "%' AND " + alias + ".commOrgIdenty like '" + orgCode + "%' ";
				}
			}else{
				if(alias.equals("")||alias==null){
					orgCodeSql = " commOrgLevel = '" + orgLevelCode + "' AND commOrgIdenty = '" + orgCode + "' ";
				}else{
					orgCodeSql = " " + alias +".commOrgLevel = '" + orgLevelCode + "' AND " + alias +".commOrgIdenty = '" + orgCode + "' ";
				}
			}
		}
		return orgCodeSql;
	}
	
	/**
	 * 集团公司内部查询
	 * @param orgLevelCode
	 * @param orgCode
	 * @param alias
	 * @param switchs
	 * @return
	 */
	public static String makeOrgCodeInSql(String orgLevelCode,String orgCode,String alias,String switchs){
		String orgCodeSql = "";
		if(switchs!=null&&switchs.equals("0")){
			orgCodeSql = " commOrgLevel like '" + orgLevelCode + "%' AND commOrgIdenty like '" + orgCode + "%' ";
		}else{
			orgCodeSql = " " + alias +".commOrgLevel like '" + orgLevelCode + "%' AND " + alias + ".commOrgIdenty like '" + orgCode + "%' ";
		}
		return orgCodeSql;
	}
	
	/**
	 * 集团全局查询
	 * @param orgLevelCode
	 * @param orgCode
	 * @param alias
	 * @param switchs
	 * @return
	 */
	public static String makeOrgCodeOutSql(String orgLevelCode,String orgCode,String alias,String switchs){
		String orgCodeSql = "";
		if(switchs!=null&&switchs.equals("0")){
			orgCodeSql = " commOrgLevel = '" + orgLevelCode + "' AND commOrgIdenty = '" + orgCode + "' ";
		}else{
			orgCodeSql = " " + alias +".commOrgLevel = '" + orgLevelCode + "' AND " + alias +".commOrgIdenty = '" + orgCode + "' ";
		}
		return orgCodeSql;
	}
	
	/**
	 * 获取版本ID的接口
	 * @return
	 * author sunhailong
	 * date   2009-10-12
	 */
	public static String makeVersionId(){
		String versionId = new Long(System.currentTimeMillis()).toString();
		return versionId;
	}
	 /**
	 * @param srcString
	 * @param flag
	 * @return
	 * @date 2009-11-24 09:56:13
	 * @author liuls
	 * @description 首字母大小写  flag  true 为首字母小写，false 为首字母大写
	 */
	public static String toLowerCaseInitial(String srcString, boolean flag) { 
         StringBuilder sb = new StringBuilder(); 
         if (flag) { 
                 sb.append(Character.toLowerCase(srcString.charAt(0))); 
         } else { 
                 sb.append(Character.toUpperCase(srcString.charAt(0))); 
         } 
         sb.append(srcString.substring(1)); 
         return sb.toString(); 
 } 
	/**
	 * @author <zhangjj>
	 * @Date <Jun 25, 2010 11:22:03 AM>
	 * @description 全模糊查询。在字段值前后都加%
	 * @param filedValue
	 * @return
	 */
	public static String addBlurry(String filedValue){
		StringBuffer str = new StringBuffer();
		str.append("%").append(filedValue).append("%");
		return str.toString();
	}
	/**
	 * @author <zhangjj>
	 * @Date <Jun 25, 2010 11:22:51 AM>
	 * @description 右模糊查询。在字段值后加%
	 * @param filedValue
	 * @return
	 */
	public static String addBlurryRight(String filedValue){
		StringBuffer str = new StringBuffer();
		str.append(filedValue).append("%");
		return str.toString();
	}
	
		/**
	 * @author <zhangjj>
	 * @Date <Jun 25, 2010 11:23:26 AM>
	 * @description 左模糊查询。在字段值前加%
	 * @param filedValue
	 * @return
	 */
	public static String addBlurryLeft(String filedValue){
		StringBuffer str = new StringBuffer();
		str.append("%").append(filedValue);
		return str.toString();
	}
	/**
	 * @author <qingfeilee>
	 * @Date <2011-08-27 14:40>
	 * @description 判断obj是否在objs数组中存在
	 * @param objs, obj
	 * @return boolean  flag
	 */
	public static boolean inArray(Object[] objs, Object obj){
		boolean flag = false;
		for (Object object : objs) {
			if((objs != null && object==obj)||(objs != null && object.equals(obj))){
				flag = true;
				break;
			}
		}
		return flag;
	}
	/**
	 * @author <qingfeilee>
	 * @Date <2011-08-27 14:40>
	 * @description 判断obj是否在objs数组中存在
	 * @param objs, obj
	 * @return boolean  flag
	 */
	public static boolean inList(List<Object> objs, Object obj){
		boolean flag = false;
		for (Object object : objs) {
			if((objs != null && object==obj)||(objs != null && object.equals(obj))){
				flag = true;
				break;
			}
		}
		return flag;
	}
	
	/**
	 * @author <qingfeilee>
	 * @Date <2011-08-27 14:40>
	 * @description 判断obj是否在objs数组中存在
	 * @param objs, obj
	 * @return boolean  flag
	 */
	public static boolean inList1(List list, Object obj){
		boolean flag = false;
		for (Object object : list) {
			if((list != null && object==obj)||(list != null && object.equals(obj))){
				flag = true;
				break;
			}
		}
		return flag;
	}
	
	/**
	 * @description 字符串编码转为GBK编码返回
	 * @Author  <chenjx>
	 * @Date    <May 21, 2012>
	 * @param str
	 * @return
	 */
	public static String toGBK(String str) {
		if (str == null)
			return "";
		else
			try {
				return new String(str.getBytes("ISO8859-1"), "GBK");
			} catch (UnsupportedEncodingException e) {
				return str;
			}
	}
	
	/**
	 * @description 字符串转换成utf-8编码返回
	 * @Author  <chenjx>
	 * @Date    <May 21, 2012>
	 * @param s
	 * @return
	 */
	public static String toUtf8String(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (c >= 0 && c <= 255) {
				sb.append(c);
			} else {
				byte[] b;
				try {
					b = Character.toString(c).getBytes("utf-8");
				} catch (Exception ex) {
					ex.printStackTrace();
					b = new byte[0];
				}
				for (int j = 0; j < b.length; j++) {
					int k = b[j];
					if (k < 0)
						k += 256;
					sb.append("%" + Integer.toHexString(k).toUpperCase());
				}
			}
		}
		return sb.toString();
	}
	
	/**
	 * @description 
	 * @Author  <chenjx>
	 * @Date    <May 21, 2012>
	 * @param line 待切割的字符串
	 * @param separator 切割符
	 * @return 返回切割后的字符串数组
	 */
	public static String[] splitBySeparator(String line, String separator) {
		// Special case when the line is empty
		if (line.length() == 0) {
			return new String[0];
		}

		int separatorLength = separator.length();
		ArrayList<String> list = new ArrayList<String>();
		int previousIndex = 0;
		int index = line.indexOf(separator);

		while (index != -1) {
			list.add(line.substring(previousIndex, index));
			previousIndex = index + separatorLength;
			index = line.indexOf(separator, previousIndex);
		}

		list.add(line.substring(previousIndex));

		String[] stringArray = new String[list.size()];
		list.toArray(stringArray);
		return stringArray;
	}
	
	/**
	 * @description 字符串过长拆分成等长的多个字符串
	 * @Author  <chenjx>
	 * @Date    <May 21, 2012>
	 * @param source 原字符串
	 * @param splitLen 拆分长度
	 * @return 返回多个等长的字符串的数组集合
	 */
	public static ArrayList<String> splitString(String source, int splitLen) {
		ArrayList<String> arry = new ArrayList<String>();
		if (source == null)
			return null;
		int loopCount;
		loopCount = (source.length() % splitLen == 0) ? (source.length() / splitLen) : (source.length() / splitLen + 1);
		String str = "";
		for (int i = 1; i <= loopCount; i++) {
			if (i == loopCount) {
				str = source.substring((i - 1) * splitLen, source.length());
				arry.add(str);
			} else {
				str = source.substring((i - 1) * splitLen, (i * splitLen));
				arry.add(str);
			}
		}
		return arry;
	}
	
	/**
	 * 验证字符串是否是IP地址
	 * @param s
	 * @return
	 */
	public static boolean validateIsIpAddr(String s){
		Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
		Matcher matcher = pattern.matcher(s); //以验证127.400.600.2为例
		//System.out.println(matcher.matches());
		boolean r=matcher.matches();
		return r;
	}
	
	/**
	 * 提取字符串中的数字,提取不到任何内容时返回空串
	 * @param str
	 * @return
	 */
	public static String getNumbersFromStr(String str){
		if(str!=null && !"".equals(str)){
			String regEx="[^0-9]";   
			Pattern p = Pattern.compile(regEx);   
			Matcher m = p.matcher(str);   
			//System.out.println( m.replaceAll("").trim());
			return m.replaceAll("").trim();
		}else{
			return "";
		}
	}
	
	public static BigDecimal getBigDecimal(Object value){
		BigDecimal ret = null;  
        if( value != null && !"".equals(value)) {  
            if( value instanceof BigDecimal ) {  
                ret = (BigDecimal) value;  
            } else if( value instanceof String ) {  
                ret = new BigDecimal( (String) value );  
            } else if( value instanceof BigInteger ) {  
                ret = new BigDecimal( (BigInteger) value );  
            } else if( value instanceof Number ) {  
                ret = new BigDecimal( ((Number)value).doubleValue() );  
            } else {  
                throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal.");  
            }  
        }else{
        	ret=new BigDecimal(0);
        }  
        return ret;  
	}
	
	/**
	 * (用\r)切割字符串并格式化
	 * @param content
	 * @return
	 */
	public static String toCut(String content){
		StringBuffer sf=new StringBuffer();
		String[] arrStr=content.split("\n");
		// \n 换行
		// \r 回车
		//LODOP.PRINT_INIT("");
		//LODOP.ADD_PRINT_TEXT(30,38,100,20,"用户ID:");
		//LODOP.ADD_PRINT_BARCODE(149,49,150,150,"QRCode","data[i].USER_NAME");
		for(int i=0;i<arrStr.length;i++){
			//去空格
			String tempStr=arrStr[i];
			tempStr=tempStr.trim().replace(" ","");//去空格
			if(tempStr.contains(",") && tempStr.contains("\"") && tempStr.contains("\"data")){
				//再用逗号进行split
				String[] tempStrArr=tempStr.split(",");
				for(int ei=0;ei<tempStrArr.length;ei++){
					//String tempEleStr:tempStrArr
					String tempEleStr=tempStrArr[ei];
					if(ei==0){
						if(tempEleStr.contains("\"") && tempEleStr.contains("data") && tempEleStr.startsWith("\"data")){
							//匹配到"data[i]"格式时
							sf.append(tempEleStr.replace("\"", ""));
						}else{
							sf.append(tempEleStr);
						}
					}else{//再将逗号补全
						if(tempEleStr.contains("\"") && tempEleStr.contains("data") && tempEleStr.startsWith("\"data")){
							//匹配到"data[i]"格式时
							sf.append(","+tempEleStr.replace("\"", ""));
						}else{
							sf.append(","+tempEleStr);
						}
					}
				}
			}else{
				sf.append(tempStr);
			}
		}
		return sf.toString();
	}
	
	/**
	 * 判断是否是移动端设备
	 * 
     * android : 所有android设备
     * mac os : iphone ipad
     * windows phone:Nokia等windows系统的手机
     * 
     * iPhone微信：
	 * User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B440 MicroMessenger/6.2.4 NetType/WIFI Language/en
	 * 
     * iPhone—safari
	 * User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12B440 Safari/600.1.4
     * 
     * Android-Chrome
	 * User-Agent:Mozilla/5.0 (Linux; Android 4.4.4; HTC D820u Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.89 Mobile Safari/537.36
     * 
     * PC-Chrome
     * User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
     * 
     * PC-Firefox
     * User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
     * 
     * PC-IE
     * User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
     * 
     */
	public static boolean checkAgentIsMobile(String requestHeader){
		String[] deviceArray = new String[]{"android","mac os","windows phone"};
        if(requestHeader == null)
        	return false;
        requestHeader = requestHeader.toLowerCase();
        for(int i=0;i<deviceArray.length;i++){
            if(requestHeader.indexOf(deviceArray[i])>0){
                return true;
            }
        }
        return false;
	}
	
}
